package com.common.app.data.bean

import android.text.TextUtils
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.contrarywind.interfaces.IPickerViewData
import java.util.*
import java.util.regex.Pattern

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2022/8/21
 * <p>
 * 描述：搜索历史
 * <p>
 * 修订历史：
 */
@Entity(tableName = "city_search")
class BaseCityModel : IPickerViewData{
    //主键
    @PrimaryKey(autoGenerate = true)
    var id = 0

    //字段
    @ColumnInfo
    var name: String = ""

    //字段
    @ColumnInfo
    var province: String = ""

    //字段
    @ColumnInfo
    var pinyin: String = ""

    //字段
    @ColumnInfo
    var code: String = ""

    //字段
    @ColumnInfo
    var level: String = ""

    //字段
    @ColumnInfo
    var pCode: String = ""

    //字段
    @ColumnInfo
    var isHistory: Boolean = false

    constructor(name: String?, province: String?, pinyin: String?,pCode: String?, code: String? = pCode) {
        if (name != null) {
            this.name = name
        }
        if (province != null) {
            this.province = province
        }
        if (pinyin != null) {
            this.pinyin = pinyin
        }
        if (pCode != null) {
            this.pCode = pCode
        }
        if (code != null) {
            this.code = code
        }
    }

    /***
     * 获取悬浮栏文本，（#、定位、热门 需要特殊处理）
     * @return
     */
    fun getSection(): String {

        return if (pinyin.isEmpty()) {
            "#"
        } else {
            val c = pinyin.substring(0, 1)
            val p = Pattern.compile("[a-zA-Z]")
            val m = p.matcher(c)
            if (m.matches()) {
                c.uppercase(Locale.getDefault())
            } else if (TextUtils.equals(c, "定") || TextUtils.equals(c, "热") || TextUtils.equals(
                    c,
                    "历"
                )
            ) pinyin else "#"
        }
    }
    /***
     * 获取悬浮栏文本，（#、定位、热门 需要特殊处理）
     * @return
     */
    fun getSection2(): String {

        return if (pinyin.isEmpty()) {
            "#"
        } else {
            pinyin
        }
    }

    override fun getPickerViewText(): String {
        return name
    }


}